/**
 * Copyright 2010 Google Inc.
 *
 * Main javascript for itinerary page.
 *
 */

goog.provide('fsl.itinerary');

goog.require('fsl.ui');

goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.json');
goog.require('goog.net.XhrIo');
goog.require('goog.ui.Button');
goog.require('goog.ui.FormPost');
goog.require('goog.ui.Ratings');
goog.require('goog.ui.Zippy');
goog.require('goog.Uri');
goog.require('goog.Uri.QueryData');

goog.require('soy');

goog.global.CLOSURE_NO_DEPS = false;


/**
 * Do-nothing ratings view.
 */
function RatingsView(opt_ratings, opt_domHelper) {
  goog.ui.Ratings.call(this, opt_ratings, opt_domHelper);
}
goog.inherits(RatingsView, goog.ui.Ratings);

RatingsView.prototype.onClick_ = function() {
}

RatingsView.prototype.onMouseOut_ = function() {
}

RatingsView.prototype.onMouseOver_ = function() {
}

RatingsView.prototype.onKeyDown_ = function() {
}


fsl.itinerary.Controller = function(uri) {
  // The page this controller is responsible for.
  this.uri_ = new goog.Uri(window.location);

  // The particular itinerary this controller is handling.
  this.itineraryKey_ = uri.getParameterValue('key');

  // Track the venues in the itinerary.
  this.itineraryVenues_ = [];

  // Map
  this.map_ = new google.maps.Map(document.getElementById('map'), {
    zoom: 11,
    center: new google.maps.LatLng(37.44, -122.14),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });
  this.infoWindow_ = new google.maps.InfoWindow();

  // Venues in "itinerary" (right column)
  this.itinerary_ = document.getElementById('itinerary');
  this.itineraryList_ = document.getElementById('itineraryList');
};

fsl.itinerary.Controller.prototype.createMap = function() {
};

fsl.itinerary.Controller.prototype.createMarker = function(venue) {
  var latLng = new google.maps.LatLng(venue.geolat, venue.geolong);
  var marker = new google.maps.Marker({
      map: this.map_,
      position: latLng,
      title: venue.name
  });
  var self = this;
  google.maps.event.addListener(marker, 'click',
    function click(event) {
        self.infoWindow_.setContent(
            soy.renderAsFragment(fsl.ui.tripsVenue, {venue: venue}));
        self.infoWindow_.open(self.map_, marker);
    }
  );
};


fsl.itinerary.Controller.prototype.addVenueToItinerary = function(venue) {
  this.itineraryVenues_.push(venue);

  // Fragment for the venue
  var venueFragment = soy.renderAsFragment(fsl.ui.tripsVenue, {venue: venue});
  var venueRating = goog.dom.getElementByClass('venue-view-rating', venueFragment);
  var venueComment = goog.dom.getElementByClass('venue-view-comment', venueFragment);

  // Add a ratings do-dad.
  var rating = new RatingsView();
  if (venue.rating) {
    rating.setSelectedIndex(venue.rating);
  }
  rating.render(venueRating);

  // List item for the fragment
  var venueLi = goog.dom.createDom('li', null, venueFragment);
  goog.dom.appendChild(this.itineraryList_, venueLi);
};

fsl.itinerary.Controller.prototype.itineraryXhr = function() {
  var queryData = new goog.Uri.QueryData();
  queryData.add('out', 'json');
  queryData.add('key', this.itineraryKey_);
  var path = 'itinerary?' + queryData.toString();

  // Send an xhr to get all trips.
  var self = this;
  goog.net.XhrIo.send(path, function(e) {
    var xhr = e.target;
    var itinerary = xhr.getResponseJson();
    for (venueIndex in itinerary.venues) {
      var venue = itinerary.venues[venueIndex];
      self.addVenueToItinerary(venue);
      self.createMarker(venue);
    }

    // Center
    self.map_.setCenter(new google.maps.LatLng(venue.geolat, venue.geolong));
  });
};

fsl.itinerary.Controller.prototype.start = function() {
  this.itineraryXhr();
};


/**
 * Entry point for /itinerary.
 */
fsl.itinerary.init = function() {
  var controller = new fsl.itinerary.Controller(new goog.Uri(window.location));
  controller.start();
};
goog.exportSymbol('fsl.itinerary.init', fsl.itinerary.init);
